package com.code.zg.news.presenters.presenter;

import com.code.zg.news.base.RxBasePresenter;
import com.code.zg.news.model.bean.WelcomeBean;
import com.code.zg.news.model.http.RetrofitHelper;
import com.code.zg.news.presenters.contracts.WelcomeContract;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by zhanggang on 2017/2/25.
 */

public class WelcomePresenter extends RxBasePresenter<WelcomeContract.View>implements WelcomeContract.Presenter {
    RetrofitHelper retrofitHelper;
    private static final String RES = "1080*1776";
    private static final int COUNT_DOWN_TIME = 2200;
    @Inject
   public WelcomePresenter(RetrofitHelper retrofitHelper){
       this.retrofitHelper=retrofitHelper;
   }
    @Override
    public void getWelcomeData() {
        Subscription welcomeBeanObservable = retrofitHelper.fetchWelcomeInfo(RES)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Action1<WelcomeBean>() {
                    @Override
                    public void call(WelcomeBean bean) {
                        mView.showContent(bean);
                        startCountDown();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.showError("");
                        mView.jumpToMain();
                    }
                });
        addSubscribe(welcomeBeanObservable);
    }

    private void startCountDown() {
        Subscription subscribe = Observable.timer(COUNT_DOWN_TIME, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {

                        mView.jumpToMain();
                    }
                });
        addSubscribe(subscribe);
    }
}